package com.zhuyh.webgis.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuyh.webgis.entity.Njod;
import com.zhuyh.webgis.model.DataGridView;
import com.zhuyh.webgis.service.INjodService;
import com.zhuyh.webgis.socket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 3247
 * @since 2020-03-11
 */
@Slf4j
@RestController
@RequestMapping("/njod")
public class NjodController {

    @Autowired
    private INjodService njodService;

    @Autowired
    private WebSocketServer webSocketServer;

    @RequestMapping(value = "/get1")
    public List<Njod> getNjod() {
        List<Njod> list = njodService.list();
        return list;
    }

    @RequestMapping(value = "/get")
    public DataGridView findAll(String page, String limit) {

        IPage<Njod> njodIPage = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));
        njodService.page(njodIPage);
        DataGridView view = new DataGridView(njodIPage.getTotal(), njodIPage.getRecords());
        return view;
    }

    @RequestMapping(value = "/qpso/{cid}")
    public Map<String, String> qpso(@PathVariable String cid) {
        Map<String, String> map = new HashMap<>();
        String result = null;
        try {
            log.info("开始执行QPSO优化");
            String[] args = new String[]{"python", "D:\\Doom\\zhuyh\\webgis\\src\\main\\resources\\code\\qpso.py"};
            Process process = Runtime.getRuntime().exec(args);
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                log.info("QPSO执行过程：{}" ,line);
                result = line;
                WebSocketServer.sendInfo(line, cid);
            }
            in.close();
            process.waitFor();
            log.info("QPSO优化执行完成");
        } catch (Exception e) {
            map.put("code", "执行失败" + e.getMessage());
            return map;
        }
        map.put("code", "执行成功,参数：" + result);
        return map;
    }

    @RequestMapping(value = "/train/{cid}")
    public Map<String,String> train(@PathVariable String cid,String param) {
        Map<String, String> map = new HashMap<>();
        System.out.println("参数：" + param);

        String result = null;
        try {
            log.info("开始训练LSTM模型");
            String[] args = new String[]{"python", "D:\\Doom\\zhuyh\\webgis\\src\\main\\resources\\code\\train.py"};
            Process process = Runtime.getRuntime().exec(args);
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                log.info("LSTM模型训练过程：{}" ,line);
                result = line;
                WebSocketServer.sendInfo(line, cid);
            }
            in.close();
            process.waitFor();
            log.info("训练LSTM模型完成");
        } catch (Exception e) {
            map.put("code", "执行失败" + e.getMessage());
            return map;
        }
        map.put("code", "LSTM模型训练完成"+result);
        return map;
    }

    @RequestMapping(value = "/predict/{cid}")
    public Map<String,String> predict(@PathVariable String cid,String param,String model) {
        Map<String, String> map = new HashMap<>();
        System.out.println("参数：" + param);

        String result = null;
        try {
            log.info("开始执行模型预测");
            String[] args = new String[]{"python", "D:\\Doom\\zhuyh\\webgis\\src\\main\\resources\\code\\predict.py"};
            Process process = Runtime.getRuntime().exec(args);
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                log.info("模型预测过程：{}" ,line);
                result = line;
                WebSocketServer.sendInfo(line, cid);
            }
            in.close();
            process.waitFor();
            log.info("模型预测完成");
        } catch (Exception e) {
            map.put("code", "执行失败" + e.getMessage());
            return map;
        }
        map.put("code", "模型预测完成"+result);
        return map;
    }

}

